<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
	
	function __construct(){
	
		parent::__construct();
		$this->load->model('User_model');
		if(!$this->session->userdata('rule')== '1' && !$this->session->userdata('username') ){
			redirect('kategori');
		}
	
	}
	
	function index(){
	
		$data['title']		= 'Data User';
		$data['main_view']	= 'user/list';
		$data['status'] = 'Data User';
		$data['query']		= $this->User_model->getUser('list',FALSE,FALSE,FAlSE);
		$this->load->view('index',$data);
	}
	
	function add(){
		$this->form_validation->set_rules('user', 'Username', 'required');
		$this->form_validation->set_rules('pass', 'Password', 'required');
		if ($this->form_validation->run() == FALSE ){
			$data['title']		= 'Tambah User Baru';
			$data['status'] = 'Tambah Data User';
			$data['main_view']	= 'user/add';
			$this->load->view('index',$data);
		}
		
		else{
			$data = array('username'	=>$this->input->post('user'),
						'password'		=>md5($this->input->post('pass')));
						
					$this->User_model->addUser($data);
					redirect('user');
		
		}
	
	}
	
	function edit(){
	
			$this->form_validation->set_rules('user', 'Username', 'required');
			$this->form_validation->set_rules('pass', 'Password', 'required');
			if ($this->form_validation->run() == FALSE ){
				$data['title']		= 'Edit User Baru';
				$data['main_view']	= 'user/edit';
				$data['status'] = 'Edit Data User';
					$id				= $this->uri->segment(3);
				$data['row']		= $this->User_model->getUser('by_id',$id,FALSE,FALSE);
				$this->load->view('index',$data);
			}
			
			else{
			$id				= $this->uri->segment(3);
				$data = array('username'	=>$this->input->post('user'),
							'password'		=>md5($this->input->post('pass')));
							
						$this->User_model->editUser($id,$data);
						redirect('user');
		}
	}
	function status(){
		$id = $this->uri->segment(4);
		$id_status = $this->uri->segment(3);
		$data = array('rule'		=> $id_status);
			$this->User_model->editUser($id,$data);
				redirect('user');
	
	}
	function delete(){
		$id = $this->uri->segment(3);
		$this->User_model->deleteUser($id);
		redirect('user');
	
	}
	

}